//
//  ReadViewController.m
//  A_Comic
//
//  Created by sos on 16/1/20.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "ReadViewController.h"
#import <AFNetworking.h>
#import "HomePageModel.h"
#import <UIImageView+WebCache.h>
#import <MBProgressHUD.h>


@interface ReadViewController ()<UIScrollViewDelegate,MBProgressHUDDelegate>
@property (nonatomic,retain) NSMutableArray *dataArr;
@property (nonatomic,retain) UIScrollView *scrollView;
@property (nonatomic,assign) BOOL isHidd;
@property (nonatomic,assign) NSInteger i;
@end

@implementation ReadViewController


- (void)dealloc
{
    [_dataArr release];
    [_scrollView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _isHidd = YES;
    //初始化i值 记录当前页数
    self.i = 1;
    // Do any additional setup after loading the view.
    self.tabBarController.tabBar.hidden = YES;
    
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.tag = 9999;
    HUD.delegate = self;
    HUD.labelText = @"loading";
    HUD.detailsLabelText = @"网络加载中 请稍后";
    HUD.dimBackground = YES;
    

    [self data];
    
    
    
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dian:)];
//    
//    [self.view addGestureRecognizer:tap];
    


    
}
-(void)dian:(UITapGestureRecognizer *)tap
{
    
    
    
    
    //判断阅读控制的显隐
    _isHidd = !_isHidd;
    UIView *newTopManager = [self.view viewWithTag:9000];
    UIView *newBottomView = [self.view viewWithTag:8000];
    if (_isHidd) {
        [UIView animateWithDuration:0.3 animations:^{
            newBottomView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height/5-20);
            
            newTopManager.frame = CGRectMake(0, -self.view.frame.size.height/11, self.view.frame.size.width, self.view.frame.size.height/11);
        }];
    }else{
        [UIView animateWithDuration:0.3 animations:^{
            
            newBottomView.frame = CGRectMake(0, self.view.frame.size.height/5*4+20, self.view.frame.size.width, self.view.frame.size.height/5-20);
            
            newTopManager.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/11);
        }];
        
    }
    
    
    
}
-(void)data
{
    

    
    NSString *url = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Comic/chapterPictureListForIos/uin/0/local_version/3.5.1/channel/1001/guest_id/FBB11919-34E9-4168-9491-811B4C0BDBD6/comic_id/%@/cid/%@/uin/0/local_version/3.5.1/channel/1001/guest_id/FBB11919-34E9-4168-9491-811B4C0BDBD6",self.str.comic_id,self.str.cid];
    NSLog(@"~~~%@",url);
    
    self.dataArr = [NSMutableArray array];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            
            MBProgressHUD *hud = [self.view viewWithTag:9999];
            [hud hide:YES afterDelay:2];
            
        }
        NSArray *arr = responseObject[@"data"][@"img_infos"];
        for (NSDictionary *dic in arr) {
            HomePageModel *m = [[HomePageModel alloc]init];
            [m setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:m.current_img_url];
            [m release];
        }
       
        CGRect h = self.view.frame;
//        h.origin.y = self.view.frame.origin.y+20;
//        h.size.height = self.view.frame.size.height-20;
        
        
        self.scrollView = [[UIScrollView alloc]initWithFrame:h];
        self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*self.dataArr.count, 0);
//        self.scrollView.backgroundColor = [UIColor redColor];
        self.scrollView.delegate = self;
        self.scrollView.pagingEnabled = YES;
        [self.view addSubview:self.scrollView];
        
     
        
        for (NSInteger i = 0; i < self.dataArr.count; i++) {
            UIScrollView *scr = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
            scr.delegate = self;
            //设置缩放比例
            //最小比例
//            scr.minimumZoomScale = 0.5;
            //最大比例
            scr.maximumZoomScale = 2;
            //设置当前比例
            scr.zoomScale = 1;
            
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.scrollView.frame.size.height)];
            img.userInteractionEnabled = YES;
            [img sd_setImageWithURL:[NSURL URLWithString:self.dataArr[i]]];
            
            
            //菜单手势点击
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dian:)];
            
            UIView *viewOne = [[UIView alloc]initWithFrame:CGRectMake(100, 100, img.frame.size.width-200, img.frame.size.height - 200)];
            viewOne.backgroundColor = [UIColor clearColor];
            [viewOne addGestureRecognizer:tap];
            
            //图片后退功能
            //后退手势点击
            UITapGestureRecognizer *houTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hou:)];
            
            UIView *viewTwo = [[UIView alloc]initWithFrame:CGRectMake(0, 100, (img.frame.size.width - viewOne.frame.size.width)/2, viewOne.frame.size.height)];
            viewTwo.backgroundColor = [UIColor clearColor];
            [viewTwo addGestureRecognizer:houTap];
            
            
            //图片前进功能
            //前进手势点击
            UITapGestureRecognizer *qianTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(qian:)];
            
            UIView *viewThree = [[UIView alloc]initWithFrame:CGRectMake(viewOne.frame.size.width+viewTwo.frame.size.width, viewOne.frame.origin.y, viewTwo.frame.size.width, viewTwo.frame.size.height)];
            viewThree.backgroundColor = [UIColor clearColor];
            [viewThree addGestureRecognizer:qianTap];
            
            
            
            
            
            
            
            [img addSubview:viewThree];
            [img addSubview:viewTwo];
            [img addSubview:viewOne];
            [scr addSubview:img];
            [self.scrollView addSubview:scr];
            [viewOne release];
            [tap release];
            [houTap release];
            [qianTap release];
            [viewTwo release];
            [viewThree release];
        }
       
        //阅读管理者
        //上控件
        
        
        UIView *topManager = [[UIView alloc]init];
        topManager.tag = 9000;
//            topManager.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/11);

            topManager.frame = CGRectMake(0, -self.view.frame.size.height/11, self.view.frame.size.width, self.view.frame.size.height/11);
        
        topManager.backgroundColor = [UIColor colorWithRed:40/255.0 green:40/255.0 blue:40/255.0 alpha:1];

        [self.view addSubview:topManager];
        [topManager release];
        
        //返回按钮
        UIButton *blackButton = [UIButton buttonWithType:UIButtonTypeCustom];
        blackButton.frame = CGRectMake(5, 5, self.view.frame.size.width/10, topManager.frame.size.height-10);
//        blackButton.backgroundColor = [UIColor blueColor];
        [blackButton addTarget:self action:@selector(black:) forControlEvents:UIControlEventTouchUpInside];
        [blackButton setTitle:@"返回" forState:UIControlStateNormal];
        [topManager addSubview:blackButton];
        
        
        //章节标签
        UILabel *numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(blackButton.frame.origin.x+blackButton.frame.size.width+5, blackButton.frame.origin.y+8, blackButton.frame.size.height, blackButton.frame.size.width)];
        numberLabel.textAlignment = NSTextAlignmentCenter;
        numberLabel.text = [NSString stringWithFormat:@"%@－",self.str.btn_str];    ;
        numberLabel.textColor = [UIColor whiteColor];
//        numberLabel.backgroundColor = [UIColor yellowColor];
        [topManager addSubview:numberLabel];
        [numberLabel release];
        
        //当前页书
        UILabel *numberOneLabel = [[UILabel alloc]initWithFrame:CGRectMake(numberLabel.frame.origin.x+numberLabel.frame.size.width, numberLabel.frame.origin.y, 27, numberLabel.frame.size.height)];
        numberOneLabel.tag = 1234;
        numberOneLabel.text = [NSString stringWithFormat:@"%ld/",(long)self.i];
        numberOneLabel.textColor = [UIColor whiteColor];
//        numberOneLabel.backgroundColor = [UIColor orangeColor];
        [topManager addSubview:numberOneLabel];
        
        //总页数
        UILabel *zongLabel = [[UILabel alloc]initWithFrame:CGRectMake(numberOneLabel.frame.origin.x+numberOneLabel.frame.size.width, numberOneLabel.frame.origin.y, numberOneLabel.frame.size.width+10, numberOneLabel.frame.size.height)];
        zongLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)self.dataArr.count];
        zongLabel.textColor = [UIColor whiteColor];
//        zongLabel.backgroundColor = [UIColor yellowColor];
        [topManager addSubview:zongLabel];
        
        
        
        //分享按钮
        UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        shareButton.frame = CGRectMake(topManager.frame.size.width-blackButton.frame.size.width, blackButton.frame.origin.y, blackButton.frame.size.width, blackButton.frame.size.height);
//        shareButton.backgroundColor = [UIColor redColor];
        [shareButton setTitle:@"分享" forState:UIControlStateNormal];
        [topManager addSubview:shareButton];
        
        
        //下控件
        
        UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height/5-20)];
        bottomView.tag = 8000;
        bottomView.backgroundColor = [UIColor colorWithRed:40/255.0 green:40/255.0 blue:40/255.0 alpha:1];
//        bottomView.alpha = 1;
        [self.view addSubview:bottomView];
        [bottomView release];
        
        //后退按钮
        UIButton * backOffButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backOffButton.backgroundColor = [UIColor blueColor];
        backOffButton.frame = CGRectMake(10, 10, bottomView.frame.size.width/10, bottomView.frame.size.height/2-20);
        backOffButton.tag = 7777;
        [backOffButton addTarget:self action:@selector(houtui:) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:backOffButton];

        
        //前进按钮
        
        UIButton *AdvanceButton = [UIButton buttonWithType:UIButtonTypeCustom];
        AdvanceButton.frame = CGRectMake(bottomView.frame.size.width-backOffButton.frame.size.width-10, backOffButton.frame.origin.y, backOffButton.frame.size.width, backOffButton.frame.size.height);
        AdvanceButton.tag = 8888;
        [AdvanceButton addTarget:self action:@selector(qianjin:) forControlEvents:UIControlEventTouchUpInside];
        AdvanceButton.backgroundColor = [UIColor redColor];
        [bottomView addSubview:AdvanceButton];
        
        
        //滑动进度条
        UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(backOffButton.frame.origin.x + backOffButton.frame.size.width+5, backOffButton.frame.origin.y, bottomView.frame.size.width-AdvanceButton.frame.size.width*2-30, AdvanceButton.frame.size.height)];
        slider.tag = 20001;
        slider.backgroundColor = [UIColor orangeColor];
        slider.minimumValue = 0;
        slider.maximumValue = (self.dataArr.count-1)*self.view.frame.size.width;
        slider.value = 1;
        

        slider.continuous = NO;
        [slider addTarget:self action:@selector(huadong:) forControlEvents:UIControlEventValueChanged];
        [bottomView addSubview:slider];
        [slider release];
        
        
        //收藏按钮
        UIButton *shoucangButton = [UIButton buttonWithType:UIButtonTypeCustom];
        shoucangButton.frame = CGRectMake(30, backOffButton.frame.origin.y+backOffButton.frame.size.height+15, (bottomView.frame.size.width-150)/4, 40);
        shoucangButton.backgroundColor = [UIColor orangeColor];
        [bottomView addSubview:shoucangButton];
        
        //亮度按钮
        UIButton *brightnessButton = [UIButton buttonWithType:UIButtonTypeCustom];
        brightnessButton.backgroundColor = [UIColor yellowColor];
        brightnessButton.frame = CGRectMake(shoucangButton.frame.origin.x+shoucangButton.frame.size.width+30, shoucangButton.frame.origin.y, shoucangButton.frame.size.width, shoucangButton.frame.size.height);
        
        [bottomView addSubview:brightnessButton];
        
        //转屏按钮
        UIButton *zhuanhuanButton = [UIButton buttonWithType:UIButtonTypeCustom];
        zhuanhuanButton.backgroundColor = [UIColor redColor];
        zhuanhuanButton.frame = CGRectMake(brightnessButton.frame.origin.x+brightnessButton.frame.size.width+30, brightnessButton.frame.origin.y, brightnessButton.frame.size.width, brightnessButton.frame.size.height);
        [bottomView addSubview:zhuanhuanButton];
        
        //上下看
        UIButton *upDownButton = [UIButton buttonWithType:UIButtonTypeCustom];
        upDownButton.backgroundColor = [UIColor brownColor];
        upDownButton.frame = CGRectMake(zhuanhuanButton.frame.origin.x+zhuanhuanButton.frame.size.width+30, zhuanhuanButton.frame.origin.y, zhuanhuanButton.frame.size.width, zhuanhuanButton.frame.size.height);
        [bottomView addSubview:upDownButton];
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
    
    
}

//返回按钮方法
-(void)black:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
    
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    
    
    //    让滚动视图上加载的第一张图片跟随缩放
    return scrollView.subviews.firstObject;
}


//图片后退点击方法
-(void)hou:(UITapGestureRecognizer *)tap
{
    UISlider *slider = [self.view viewWithTag:20001];
    UILabel *label = [self.view viewWithTag:1234];
    if (self.scrollView.contentOffset.x <= 0) {
    
//        self.scrollView.contentOffset = CGPointMake(0, 0);

        UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:@"亲!已经是第一张了" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:aler animated:YES completion:^{
            
        
        }];
        [self performSelector:@selector(yanshi) withObject:aler afterDelay:1];

        
        NSLog(@"第一张");
    }else{
        slider.value -= self.view.frame.size.width;
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x-self.view.frame.size.width, 0) animated:NO];
        self.i--;
        label.text = [NSString stringWithFormat:@"%ld/",(long)self.i];
    }

    
}

//后退按钮方法
-(void)houtui:(UIButton *)btn
{
    
    
    if ( self.str.btn_str.integerValue !=1) {
        
        [self.navigationController popViewControllerAnimated:NO];
        [self.delegater houDian:self.number - self.str.btn_str.integerValue];
    }else{
        UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:@"亲!再点就撞到南墙了" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:aler animated:YES completion:^{
            
            
        }];

        [self performSelector:@selector(yanshi) withObject:aler afterDelay:1];

    }
    
   
}


//图片前进点击方法
-(void)qian:(UITapGestureRecognizer *)tap
{
    UISlider *slider = [self.view viewWithTag:20001];
    UILabel *label = [self.view viewWithTag:1234];

    if (self.scrollView.contentOffset.x != self.view.frame.size.width*(self.dataArr.count-1)) {
        self.i++;
        slider.value += self.view.frame.size.width;
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x+self.view.frame.size.width, 0) animated:NO];
        label.text = [NSString stringWithFormat:@"%ld/",(long)self.i];

    }else{
        
        
        
        UIButton *btn = [self.view viewWithTag:8888];
        if (self.str.btn_str.integerValue != self.number) {
            
            [self qianjin:btn];
        }else{
            
            UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:@"亲!暂无后续" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:aler animated:YES completion:^{
                
                
            }];
            
            [self performSelector:@selector(yanshi) withObject:aler afterDelay:1];
            
            
        }
        NSLog(@"最后一张");
        
    }
}

//前进按钮方法
-(void)qianjin:(UIButton *)btn
{
    if (self.number != self.str.btn_str.integerValue) {
        
        [self.navigationController popViewControllerAnimated:NO];
        [self.delegater houDianTwo:self.number - self.str.btn_str.integerValue];
    }else{
        UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:@"亲!暂无后续" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:aler animated:YES completion:^{
            
            
        }];
        
        [self performSelector:@selector(yanshi) withObject:aler afterDelay:1];
        
        
    }

    
    



}
//滑动控制器触发方法
-(void)huadong:(UISlider *)slider
{
    
    NSInteger distance = (NSInteger)slider.value % (NSInteger)self.view.frame.size.width;
    NSInteger page = (NSInteger)slider.value / self.view.frame.size.width;
    UILabel *labelOne = [self.view viewWithTag:1234];
    
    if (distance <= self.view.frame.size.width / 2) {
         [self.scrollView setContentOffset:CGPointMake(self.view.frame.size.width * page, 0) animated:YES];
        slider.value = self.view.frame.size.width * page;
        labelOne.text = [NSString stringWithFormat:@"%ld/",page+1];
        self.i = page+1;
    } else {
         [self.scrollView setContentOffset:CGPointMake(self.view.frame.size.width * (page+1), 0) animated:YES];
        slider.value = self.view.frame.size.width * (page+1);
        labelOne.text = [NSString stringWithFormat:@"%ld/",page+2];
        self.i = page+2;

            }
    


    
}

//延时方法
-(void)yanshi
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
